package net.wanho.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import net.wanho.hospital.po.Arrange;
import net.wanho.hospital.po.Doctor;
import net.wanho.hospital.mapper.DoctorUserMapper;
import net.wanho.hospital.po.Orders;
import net.wanho.hospital.service.ArrangeService;
import net.wanho.hospital.service.DoctorUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.hospital.util.JwtUtil;
import net.wanho.hospital.vo.DoctorVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-12-27
 */
@Service
@RequiredArgsConstructor
public class DoctorUserServiceImpl extends ServiceImpl<DoctorUserMapper, Doctor> implements DoctorUserService {

    private final ArrangeService arrangeService;

   /**
    *   按查询分页查询
    */
    @Override
    public IPage<Doctor> findPage(int pageNum, int pageSize, Doctor doctor) {
        QueryWrapper<Doctor> wrapper = null;
        IPage<Doctor> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public DoctorVo login(int dId, String dPassword) {
        Doctor doctor = this.getById(dId);


        Map<String,Object> payload = new HashMap<>();
        payload.put("dId",doctor.getDId());
        payload.put("dName",doctor.getDName());
        String token = JwtUtil.generate(payload);

        DoctorVo doctorVo = new DoctorVo();
        doctorVo.setDId(doctor.getDId());
        doctorVo.setDName(doctor.getDName());
        doctorVo.setToken(token);

        return doctorVo;
    }

    @Override
    public Map<String, Object> findDoctorBySectionPage(int pageNum, int pageSize, String dName, String dSection, String arrangeDate) {
        /**
         * 按条件分页查询医生信息
         */
        Page<Doctor> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Doctor> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Doctor::getDName, dName)
                .eq(Doctor::getDSection, dSection);

        this.page(page, wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("doctors", page.getRecords());
        map.put("total", page.getTotal());

        /**
         * 查询医生是否已排班
         */
        page.getRecords().forEach(doctor -> {
            LambdaQueryWrapper<Arrange> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(Arrange::getDId, doctor.getDId())
                    .eq(Arrange::getArTime, arrangeDate);
            Arrange arrange = arrangeService.getOne(wrapper1);
            if (arrange != null){
                doctor.setArrangeId(arrange.getArId());
            }
        });


        return map;
    }

    @Override
    public Doctor findDoctor(Integer dId) {
        return this.getById(dId);
    }

    @Override
    public void updateStar(Integer dId, Integer dStar) {
        this.getBaseMapper().updateStar(dId, dStar);
    }


}
